package com.yao.sys.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.common.bean.PageQueryBean;
import com.yao.common.helper.ValidateHelper;
import com.yao.common.resp.ApiResponse;
import com.yao.sys.entity.dto.RoleDto;
import com.yao.sys.service.RoleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 * 角色管理
 *
 * @author autoCode
 * @version 1.0.0-SNAPSHOT
 * @date 2020-04-09
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping(value = "/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "新增角色", notes = "新增角色")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ApiResponse<Object> add(@RequestBody RoleDto model) {

        ValidateHelper.validateNull(model, new String[] {"name", "keys"});

        roleService.add(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "修改角色", notes = "修改角色")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiResponse<Object> update(@RequestBody RoleDto model) {

        ValidateHelper.validateNull(model, new String[] {"id", "name", "keys"});

        roleService.modifyById(model);

        return ApiResponse.success();
    }

    @ApiOperation(value = "删除角色", notes = "逻辑删除角色")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public ApiResponse<Object> deleteById(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        roleService.removeById(id);

        return ApiResponse.success();
    }

    @ApiOperation(value = "获取角色", notes = "获取角色")
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    public ApiResponse<RoleDto> get(@PathVariable("id") Integer id) {

        ValidateHelper.validateId(id);

        return ApiResponse.success(roleService.byId(id));
    }

    @ApiOperation(value = "角色列表(分页)", notes = "角色列表(分页)")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResponse<IPage<RoleDto>> list(@RequestBody PageQueryBean<RoleDto> queryBean) {

        return ApiResponse.success(roleService.page(new Page<>(queryBean.getCurrent(), queryBean.getSize())));
    }

    @ApiOperation(value = "角色列表", notes = "角色列表")
    @RequestMapping(value = "/listAll", method = RequestMethod.GET)
    public ApiResponse<List<RoleDto>> listAll() {

        return ApiResponse.success(roleService.list());
    }

}